package service

import (
	"errors"
	"go_my_framework/app/dao"
	"go_my_framework/app/global/request"
	"go_my_framework/app/model"
)

type RoleService struct {
}

// 角色列表
func (r *RoleService) GetRoleList(f request.RoleForm) ([]model.Role, int64, error) {
	if f.Page == 0 {
		f.Page = 1
	}
	if f.Limit == 0 {
		f.Limit = 10
	}
	data, count, err := dao.NewRoleDaoTmpl().GetList(f.Page, f.Limit, f.Name)
	if err != nil {
		return []model.Role{}, 0, errors.New(err.Error())
	}
	return data, count, nil
}

func (r *RoleService) AddRole(name string, desc string, super int8) error {
	return dao.NewRoleDaoTmpl().Create(name, desc, super)
}

func (r *RoleService) EditRole(id int, name string, desc string, super int8) error {
	return dao.NewRoleDaoTmpl().Update(id, name, desc, super)
}

func (r *RoleService) FindRole(id int) (model.Role, error) {
	data, err := dao.NewRoleDaoTmpl().Find(id)
	if err != nil {
		return model.Role{}, nil
	}
	return data, nil
}

func (r *RoleService) DelRole(id int) error {
	return dao.NewRoleDaoTmpl().Del(id)
}